package com.lj.framework.common.support;


import com.alibaba.fastjson2.filter.ContextAutoTypeBeforeHandler;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import com.alibaba.fastjson2.JSON;

public class GenericFastJsonRedisSerializer implements RedisSerializer<Object> {
    ContextAutoTypeBeforeHandler contextFilter;

    public GenericFastJsonRedisSerializer() {
        this.contextFilter = null;
    }

    public GenericFastJsonRedisSerializer(String[] acceptNames) {
        this.contextFilter = new ContextAutoTypeBeforeHandler(acceptNames);
    }

    public byte[] serialize(Object object) throws SerializationException {
        if (object == null) {
            return new byte[0];
        } else {
            try {
                return JSON.toJSONBytes(object);
            } catch (Exception var3) {
                throw new SerializationException("Could not serialize: " + var3.getMessage(), var3);
            }
        }
    }

    public Object deserialize(byte[] bytes) throws SerializationException {
        if (bytes != null && bytes.length != 0) {
            try {
                return JSON.parseObject(bytes, Object.class, this.contextFilter);
            } catch (Exception var3) {
                throw new SerializationException("Could not deserialize: " + var3.getMessage(), var3);
            }
        } else {
            return null;
        }
    }
}